// var catalog_Item = require('../../detail/catalog-item')

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iscatalogitem: [false, false, false, false, false],
    id: 2,
    item: [],
    service: [],
    beauty: [],
    popular: [], //网红爆品
    daily_expenses: [], //日常用品 
    snacks: [] //零食
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      items: app.globalData.product, //产品
      // service: app.globalData.service_items //上门服务
    })
    var that = this
    let tableName = 'youhui'

    let Query = new wx.BaaS.Query()
    // Query.compare('hot', '=', parseInt(1))
    let Product = new wx.BaaS.TableObject(tableName)
    Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
      // success
      console.log(res)
      that.setData({
        service: res.data.objects, //热门商品
      })
      // app.globalData.key = this.data.key
      // console.log(that.data.key)
    }, err => {
      // err
    })
    this.onBeauty_Checked() //美妆产品
    this.onDaily_expenses_Checked() //生活日用
    this.onPopular_Checked() //网红爆品
    this.onSnacks_Checked() //零食
    this.setData({
      iscatalog2: true
    })

  },


  GOsss:function(e){
    console.log(e.currentTarget.dataset.postid)
    app.globalData.youhui = this.data.service[e.currentTarget.dataset.postid]
    app.goyouhui(e.currentTarget.dataset.postid)
  },
  onBeauty_Checked() {
    //美妆产品
    var that = this
    var item = []
    for (var i = 0; i < app.globalData.product_sum; i++) {
      if (that.data.items[i].beauty == 1) {
        item.push(that.data.items[i])
      }
    }
    console.log(item)
    that.setData({
      beauty: item, //美妆产品全部数据
    })
  },

  onSnacks_Checked() {
    //零食
    var that = this
    var item = this.data.daily_expenses
    for (var i = 0; i < app.globalData.product_sum; i++) {
      if (that.data.items[i].snacks == 1) {
        item.push(that.data.items[i])
      }
    }
    console.log(item)
    that.setData({
      daily_expenses: item, //美妆产品全部数据
    })
  },
  onDaily_expenses_Checked() {
    //生活日用
    var that = this
    var item = []
    for (var i = 0; i < app.globalData.product_sum; i++) {
      if (that.data.items[i].daily_expenses == 1) {
        item.push(that.data.items[i])
      }
    }
    console.log(item)
    that.setData({
      daily_expenses: item, //美妆产品全部数据
    })
  },
  onPopular_Checked() {
    //网红爆品
    var that = this
    
    var that = this
    var item = []
    for (var i = 0; i < app.globalData.product_sum; i++) {
      if (that.data.items[i].popular == 1) {
        item.push(that.data.items[i])
      }
    }
    that.setData({
      popular: item, //网红爆品
    })
  },
  onTap: function (event) {
    var id = event.currentTarget.id
    var iscatalogitem = this.data.iscatalogitem
    console.log(id)
    for (var i = 0; i < 5; i++) {
      if (i == (id - 1)) {
        iscatalogitem[id - 1] = true
      } else {
        iscatalogitem[i] = false
      }
    }
    this.setData({
      iscatalog1: iscatalogitem[0],
      iscatalog2: iscatalogitem[1],
      iscatalog3: iscatalogitem[2],
      iscatalog4: iscatalogitem[3],
      iscatalog5: iscatalogitem[4],
    })
  },
  onSerchtap: function () {
    wx.navigateTo({
      url: '../search/search'

    })
  },
  GoBeautyDetail: function (e) {
    var that = this
    var index = e.currentTarget.dataset.postid
    console.log(index)
    console.log(this.data.beauty[index].product_code)
    setTimeout(function (e) {
      app.navigateToDeTail(that.data.beauty[index].product_code)
    }, 500)
  },
  GoSnacksDetail: function (e) {
    var that = this
    var index = e.currentTarget.dataset.postid
    console.log(index)
    console.log(this.data.snacks[index].product_code)
    setTimeout(function (e) {
      app.navigateToDeTail(that.data.seauty[index].product_code)
    }, 500)
  },
  GoPopularDetail: function (e) {
    var that = this
    var index = e.currentTarget.dataset.postid
    console.log(index)
    console.log(this.data.popular[index].product_code)
    setTimeout(function (e) {
      app.navigateToDeTail(that.data.popular[index].product_code)
    }, 500)
  },

  GoDaily_expensesDetail: function (e) {
    var that = this
    var index = e.currentTarget.dataset.postid
    console.log(index)
    console.log(this.data.daily_expenses[index].product_code)
    setTimeout(function (e) {
      app.navigateToDeTail(that.data.daily_expenses[index].product_code)
    }, 500)
  },
  GoServiceDetail: function (e) {
    var that = this
    var index = e.currentTarget.dataset.postid
    console.log(index)
    console.log(this.data.service[index].item_name)
    setTimeout(function (e) {
      app.navigateToService(that.data.service[index].item_name)
    }, 500)
  },
})